<?php

/**
 * address actions.
 *
 * @package    wildlive
 * @subpackage address
 * @author     Max Lenkeit
 * @version    SVN: $Id: actions.class.php 23810 2009-11-12 11:07:44Z Kris.Wallsmith $
 */
class addressActions extends sfActions
{
  public function executeIndex(sfWebRequest $request)
  {
    $this->addresss = Doctrine_Core::getTable('address')
      ->createQuery('a')
      ->execute();
  }

  public function executeNew(sfWebRequest $request)
  {
    $this->form = new addressForm();
  }

  public function executeCreate(sfWebRequest $request)
  {
    $this->forward404Unless($request->isMethod(sfRequest::POST));

    $this->form = new addressForm();

    $this->processForm($request, $this->form);

    $this->setTemplate('new');
  }

  public function executeEdit(sfWebRequest $request)
  {
    $this->forward404Unless($address = Doctrine_Core::getTable('address')->find(array($request->getParameter('id'))), sprintf('Object address does not exist (%s).', $request->getParameter('id')));
    $this->form = new addressForm($address);
  }

  public function executeUpdate(sfWebRequest $request)
  {
    $this->forward404Unless($request->isMethod(sfRequest::POST) || $request->isMethod(sfRequest::PUT));
    $this->forward404Unless($address = Doctrine_Core::getTable('address')->find(array($request->getParameter('id'))), sprintf('Object address does not exist (%s).', $request->getParameter('id')));
    $this->form = new addressForm($address);

    $this->processForm($request, $this->form);

    $this->setTemplate('edit');
  }

  public function executeDelete(sfWebRequest $request)
  {
    $request->checkCSRFProtection();

    $this->forward404Unless($address = Doctrine_Core::getTable('address')->find(array($request->getParameter('id'))), sprintf('Object address does not exist (%s).', $request->getParameter('id')));
    $address->delete();

    $this->redirect('address/index');
  }

  protected function processForm(sfWebRequest $request, sfForm $form)
  {
    $form->bind($request->getParameter($form->getName()), $request->getFiles($form->getName()));
    if ($form->isValid())
    {
      $address = $form->save();

      $this->redirect('address/edit?id='.$address->getId());
    }
  }
  
  protected function processAddressbookSearch(sfWebRequest $request, sfForm $form) {
  	$form->bind($request->getParameter($form->getName()), $request->getFiles($form->getName()));
  	$this->keyword = $form->getValue('keyword');
  	$members = AddressbookPeer::getInstance()->findMembersByKeyword($this->keyword);
  	$addresses = AddressbookPeer::getInstance()->findAddressesByKeyword($this->keyword);
  	$phonecontacts = AddressbookPeer::getInstance()->findPhoneContactsByKeyword($this->keyword);
  	$results = array();
  	if($members instanceof Doctrine_Collection)
  		$results = array_merge($results, $members->getData());
  	if($addresses instanceof Doctrine_Collection)
  		$results = array_merge($results, $addresses->getData());
  	if($phonecontacts instanceof Doctrine_Collection)
  		$results = array_merge($results, $phonecontacts->getData());
  	$this->results = $results;
  }
  
  public function executeSearch(sfWebRequest $request) {
  	$form = new AddressbookSearchForm();
  	if($request->isMethod(sfRequest::POST)) { 
  		$this->processAddressbookSearch($request, $form);
  	}
  	$this->form = $form;
  }
}
